#include "Enemy.h"
#include "TextureLoader.h"
#include "Game.h"
#include <list>
#include "Bullet.h"
#include "GameScene.h"

Enemy::Enemy(int x, int y, EnemyType type,GameScene* scene)
    : Tank(x, y, MAP_BLOCK_WIDTH, MAP_BLOCK_HEIGHT, Angle::DOWN, 0.12f,scene), type_(type), time_(0),isSwitch_(false)
{
    if(type == EnemyType::Fast)
        speed_ = 0.15f;
}

Enemy::~Enemy()
{
    
}

void Enemy::Update(float dt)
{
    time_ += dt;
    if(time_ >= 1200)
    {
        time_ = 0;
        //随机转向
        int dir = Game::GetInstance().GetRandom(0, 3);
        angle_ = (Angle)dir;
        shoot();
    }
    Move(dt);
    isSwitch_ = !isSwitch_;
}

void Enemy::Render()
{
    auto texture = TextureLoader::Instance().GetEnemyTexture(angle_,type_,isSwitch_);
    SDL_RenderCopy(Game::GetInstance().GetRenderer(), texture.first, &texture.second, &GetRect());
}

void Enemy::shoot()
{
    if (bullet_.expired())
    {
        auto bullet = std::make_shared<Bullet>(x_ + width_/2 - BULLET_SIZE/2, y_ + height_/2 - BULLET_SIZE/2, BULLET_SIZE, BULLET_SIZE, angle_, 0.3f, scene_);
        bullet->isEnemyBullet_ = true;
        bullet_ = bullet;
        scene_->GetBullets().push_back(bullet_.lock());
    }
}
